#include "ObBrickSP.h"
#include "ObMushRoomEdible.h"
#include "ObFlowerEdible.h"
#include "ObStarEdible.h"
#include "ObPlayer.h"
#include "D3DApp.h"
#include "Collision.h"
#include "MapScene.h"

IDirect3DTexture9* ObBrickSP::mTex		= 0;
IDirect3DTexture9* ObBrickSP::mStuffTex = 0;
IDirect3DTexture9* ObBrickSP::mCoinTex	= 0;
CSound* ObBrickSP::sBonusAppear			= 0;
CSound* ObBrickSP::sCoin				= 0;

ObBrickSP::ObBrickSP(){
	setId(BRICKSP1);
	setInfo(NOTHING);
}

ObBrickSP::ObBrickSP(BRICKSP nid,  INFO ninfo){
	setId(nid);
	setInfo(ninfo);
}

ObBrickSP::~ObBrickSP(){

}

bool ObBrickSP::setId(int nid){
	if(checkType(nid)){
		mID = nid;
		mWidth	= 16.0f;
		mHeight = 16.0f;
		mCoin = 0;
		mCanKill = false;
		mTriggerObject = 0;
		mCenter = D3DXVECTOR3(mWidth/2, mHeight/2, 0.0f);
		mSpritePos.x = 8;
		mSpritePos.y = 8;
		int x = mID-40;
		mSpritePos.x +=x*16;
		delaycount = 0.0f;
		return true;
	}else{
		return false;
	}
}

bool ObBrickSP::setInfo(int ninfo){
	if(checkInfo(ninfo)){
		mInfo = ninfo;
		switch (mInfo)
		{
		case ObBrickSP::TIEN:
			mCoin = 1;
			break;
		case ObBrickSP::TIEN10:
			mCoin = 10;
			break;
		case ObBrickSP::TIEN12:
			mCoin = 12;
			break;
		case ObBrickSP::TIEN20:
			mCoin = 20;
			break;
		default:
			if(mCoin > 0) return true;
			mCoin = 0;
			break;
		}
		return true;
	}else{
		return false;
	}
}

bool ObBrickSP::checkType(int obID){
	if(obID<BRICKSP1 || obID>BRICKSP4){
		return false;
	}else{
		return true;
	}
}

bool ObBrickSP::checkInfo(int obID){
	if(obID<NAM || obID>NOTHING){
		return false;
	}else{
		return true;
	}
}

bool ObBrickSP::getCanKill(){
	return mCanKill;
}
void ObBrickSP::setCanKill(bool cankill){
	mCanKill = cankill;
}

void ObBrickSP::collidedWith(ObObject* object, CollisionRS rs){
	switch (object->getId())
	{
	case ObPlayer::MARIO:
		if(ObPlayer* player  = dynamic_cast<ObPlayer*>(object)){
			if(rs.mState == Bottom){
				if(mInfo != NOTHING){
					mCanKill = true;
					delaycount = 0.05f;
				}
				switch (mInfo)
				{
				case NAM:
					gMapScene->mNewObject.push_back(new ObMushroomEdible(mStuffTex,sBonusAppear , mPos.x,mPos.y,false));
					setInfo(NOTHING);
					break;
				case HOA:
					gMapScene->mNewObject.push_back( new ObFlowerEdible(mStuffTex,mPos.x,mPos.y));
					setInfo(NOTHING);
					break;
				case SAO:
					gMapScene->mNewObject.push_back(new ObStarEdible(mStuffTex,mPos.x,mPos.y));
					setInfo(NOTHING);
					break;
				case NAMHOA:
					if(player->getStatus() == SMALL){
						gMapScene->mNewObject.push_back( new ObMushroomEdible(mStuffTex,sBonusAppear,mPos.x,mPos.y,false));
						setInfo(NOTHING);
					}else{
						gMapScene->mNewObject.push_back( new ObFlowerEdible(mStuffTex,mPos.x,mPos.y));
						setInfo(NOTHING);
					}
					break;
				case TIEN:
					mCoin--;
					setInfo(NOTHING);
					PlaySound(sCoin);
					player->setScore(player->getScore() +20);
					mList.push_back(new ObCoinUp(mCoinTex,mPos.x,mPos.y,0,500,0.6f));
					break;
				case TIEN10:
					mCoin--;
					setInfo(NOTHING);
					PlaySound(sCoin);
					player->setScore(player->getScore() +20);
					mList.push_back(new ObCoinUp(mCoinTex,mPos.x,mPos.y,0,500,0.6f));
					break;
				case TIEN12:
					mCoin--;
					setInfo(NOTHING);
					PlaySound(sCoin);
					mList.push_back(new ObCoinUp(mCoinTex,mPos.x,mPos.y,0,500,0.6f));
					break;
				case TIEN20:
					mCoin--;
					setInfo(NOTHING);
					PlaySound(sCoin);
					player->setScore(player->getScore() +20);
					mList.push_back(new ObCoinUp(mCoinTex,mPos.x,mPos.y,0,500,0.6f));
					break;
				case NAMLV:
					gMapScene->mNewObject.push_back( new ObMushroomEdible(mStuffTex,sBonusAppear,mPos.x,mPos.y,true));
					setInfo(NOTHING);
					break;
				case NOTHING:
					if(mCoin == 0) return;
					mCoin--;
					PlaySound(sCoin);
					mList.push_back(new ObCoinUp(mCoinTex,mPos.x,mPos.y,0,500,0.6f));
					break;
				default:
					break;
				}
				if(mInfo == NOTHING && mSpritePos.y == 8.0f && mCoin == 0) mSpritePos.y +=16.0f;
				//if(mCoin <= 0 ){
				//	setInfo(NOTHING);
				//}
			}
		}
		break;
	default:
		break;
	}
}

void ObBrickSP::update(float dt){
	if(mNeedDispose) return;

	if(mCanKill){
		delaycount-=dt;
		if(delaycount<=0.0f){
			delaycount = 0.0f;
			mCanKill = false;
		}
	}

	bool notdone = false;
	for(std::vector<ObCoinUp*>::size_type i = 0; i < mList.size();i++){
		if(mList[i]->mNeedDispose == false){
			notdone = true;
		}
	}

	if(notdone == false){
		for(std::vector<ObCoinUp*>::size_type i = 0; i < mList.size();i++){
			mList[i]->clear();
			delete mList[i];
		}
		mList.clear();
	}

	if(mTriggerObject != 0){
		mTriggerObject->update(dt);
	}

	for(std::vector<ObCoinUp*>::size_type i = 0; i < mList.size();i++){
		mList[i]->update(dt);
	}
	
}

void ObBrickSP::render(ID3DXSprite* mSprite){
	if(mID == 0) return;
	if(mTex == 0) return;
	if(mNeedDispose) return;

	for(std::vector<ObDynamic*>::size_type i = 0; i < mList.size();i++){
		mList[i]->render(mSprite);
	}

	if(mTriggerObject != 0){
		mTriggerObject->render(mSprite);
	}

	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x,mPos.y - gCamera->getPos().y,0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
	mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false));
}

bool ObBrickSP::loadTexture(){
	if(mTex == 0){
		HR(D3DXCreateTextureFromFileEx(
			gDevice,
			"Sprites/BrickSP.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex));
	}

	if(mStuffTex == 0){
		HR(D3DXCreateTextureFromFileEx(
			gDevice,
			"Sprites/BrickSPStuff.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mStuffTex));
	}

	if(mCoinTex == 0){
		HR(D3DXCreateTextureFromFileEx(
			gDevice,
			"Sprites/Coin.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mCoinTex));
	}

	sBonusAppear = LoadSound("Sounds\\BonusAppear.wav");
	sCoin = LoadSound("Sounds\\GetCoin.wav");
	return true;
}

bool ObBrickSP::releaseTexture(){
	if(sBonusAppear != 0){
		delete sBonusAppear;
	}

	if(sCoin != 0){
		delete sCoin;
	}

	if(mTex!=0){
		ReleaseCOM(mTex);
	}else{
		return false;
	}

	if(mStuffTex!=0){
		ReleaseCOM(mStuffTex);
	}else{
		return false;
	}

	if(mCoinTex!=0){
		ReleaseCOM(mCoinTex);
	}else{
		return false;
	}
	return true;
}